package com.app.web.servlets;

import com.app.common.AppException;
import com.app.common.ResponseResult;
import com.app.pojo.Department;
import com.app.service.IDepartmentService;
import com.app.service.impl.DepartmentService;
import com.app.utils.UtilTools;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Copyright (C) @2020 fuguo.wang@logicsolutions.com.cn
 *
 * @author fuguo.wang
 * @version 1.0
 * @className DepartmentServlet
 * @date 2020-06-17 22:02
 * @description 部门控制层
 */
@WebServlet(name = "部门控制层", urlPatterns = "/departments")
public class DepartmentServlet extends HttpServlet {
	private static final long serialVersionUID = - 1876165254316789965L;

	private final IDepartmentService service = new DepartmentService();


	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) {
		switch (request.getParameter("type")) {
			case "view":
			case "edit":
				String id = request.getParameter("id");
				try {
					Department department = service.get(Integer.parseInt(id));
					// 操作成功
					ResponseResult.response(request, response, department, "departments/detail.jsp");
				} catch (AppException e) {
					// 提示操作失败
					ResponseResult.warn(request, response, "操作失败！", e.getMessage(), 1.2f, "departments?type=list");
				} catch (Exception e) {
					e.printStackTrace();
					// 系统异常
					ResponseResult.error(request, response, null, e, 2.5f, "index.jsp");
				}
				break;
			case "delete":
				// 获取数据主键
				id = request.getParameter("id");
				try {
					// service 交互  去删除
					service.delete(id);
					ResponseResult.info(request, response, "操作提示！", "删除成功！", 1f, "departments?type=list");
				} catch (AppException e) {
					ResponseResult.warn(request, response, "操作失败！", e.getMessage(), 1f, "departments?type=list");
				} catch (Exception e) {
					e.printStackTrace();
					ResponseResult.error(request, response, null, e, 2.5f, "index.jsp");
				}
				break;
			default:
				try {
					List<Department> departments = service.get();
					ResponseResult.response(request, response, departments, "departments/list.jsp");
				} catch (AppException e) {
					// 提示操作失败
					ResponseResult.warn(request, response, "操作失败！", e.getMessage(), 3f, "index.jsp");
				} catch (Exception e) {
					e.printStackTrace();
					// 系统异常
					ResponseResult.error(request, response, null, e, 2.5f, "index.jsp");
				}
				break;
		}

	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) {
		switch (request.getParameter("type")) {
			case "new":
				try {
					Department department = this.mapperObj(request);
					service.add(department);
					ResponseResult.info(request, response, "操作提示！", "新增成功！", 1.8f, "departments?type=list");
				} catch (AppException e) {
					// 提示操作失败
					ResponseResult.warn(request, response, "操作失败！", e.getMessage(), 3f, "departments?type=list");
				} catch (Exception e) {
					e.printStackTrace();
					// 系统异常
					ResponseResult.error(request, response, null, e, 2.5f, "index.jsp");

				}
				break;
			case "edit":
				try {
					Department department = this.mapperObj(request);
					service.update(department);
					ResponseResult.info(request, response, "操作提示", "部门信息更新成功", 1.5f, "departments?type=list");
				} catch (AppException e) {
					// 提示操作失败
					ResponseResult.warn(request, response, "操作失败！", e.getMessage(), 3f, "departments?type=list");
				} catch (Exception e) {
					e.printStackTrace();
					// 系统异常
					ResponseResult.error(request, response, null, e, 2.5f, "index.jsp");
				}
				break;
		}
	}

	/**
	 * 从request请求中取值  封装为对象
	 *
	 * @param request
	 * @return
	 */
	private Department mapperObj(HttpServletRequest request) {
		String id = request.getParameter("id");
		String disable = request.getParameter("disable");
		String code = request.getParameter("code");
		String name = request.getParameter("name");
		String address = request.getParameter("address");

		try {
			return new Department()
					.setCode(code)
					.setName(name)
					.setAddress(address)
					.setId(UtilTools.convert(id,Integer.class))
					.setDisable("1".equals(disable));
		} catch (NumberFormatException e) {
			e.printStackTrace();
			throw new AppException(e);
		}
	}


}
